﻿using System;
using System.Text;
using System.Collections;

using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Bpms.Control
{
    /// <summary>
    /// 文本框控件
    /// </summary>
    [DefaultProperty("Text"), ToolboxData("<{0}:TextBox runat=server></{0}:TextBox>"), Designer("System.Web.UI.Design.WebControls.PreviewControlDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    public class TextBox : System.Web.UI.WebControls.TextBox, IControl
    {

        // RequiredFieldValidator控件变量
        RequiredFieldValidator rfv = new RequiredFieldValidator();

        // RegularExpressionValidator控件变量
        RegularExpressionValidator rev = new RegularExpressionValidator();

        // RangeValidator控件变量
        RangeValidator rv = new RangeValidator();

        // CompareValidator控件变量
        CompareValidator cv = new CompareValidator();

        /// <summary>
        /// 构造函数
        /// </summary>
        public TextBox()
            : base()
        {

        }

        protected override void CreateChildControls()
        {
            // base.CreateChildControls();
            #region   验证准备
            bool isRfv = false;  //是否需要 RequiredFieldValidator 验证
            bool isCv = false;   //是否需要 CompareValidator 验证
            bool isRv = false;   //是否需要 RangeValidator 验证
            bool isRev = false; //是否需要 RegularExpressionValidator 验证
            #endregion

            #region  RequiredFieldValidator 验证

            switch (CanBeNull)
            {
                case "可为空":
                    {
                        break;
                    }
                case "必填":
                    {
                        rfv.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
                        rfv.ControlToValidate = this.ID;
                        rfv.ErrorMessage = "<font color=red>请输入内容!</font>";
                        // rfv.ErrorMessage = this.Text;
                        // this.Controls.AddAt(0, rfv);
                        isRfv = true;
                        break;
                    }
                default:
                    {
                        break;
                    }
            }


            #endregion

            #region  CompareValidator 验证
            //密码
            if (this.TextMode == TextBoxMode.Password && this.ControlToCompare != null && this.ControlToCompare != "")
            {
                cv.ControlToValidate = this.ID;
                cv.ControlToCompare = this.ControlToCompare;
                cv.Operator = ValidationCompareOperator.Equal;
                cv.ErrorMessage = "两次输入的密码不一致！";
                isCv = true;
                // this.Controls.AddAt(0, cv);
            }

            #endregion

            #region  RangeValidator 验证

            //当指定了输入框的最小或最大值时,则加入校验范围项
            if (this.MaximumValue != null || this.MinimumValue != null)
            {
                rv.ControlToValidate = this.ID;
                rv.Type = ValidationDataType.Double;

                if (this.MaximumValue != null && this.MinimumValue != null)
                {
                    rv.MaximumValue = this.MaximumValue;
                    rv.MinimumValue = this.MinimumValue;
                    rv.ErrorMessage = "当前输入数据应在" + this.MinimumValue + "和" + this.MaximumValue + "之间!";
                }
                else
                {
                    if (this.MaximumValue != null)
                    {
                        rv.MaximumValue = this.MaximumValue;
                        rv.MinimumValue = Int32.MinValue.ToString();
                        rv.ErrorMessage = "当前输入数据允许最大值为" + this.MaximumValue;
                    }
                    if (this.MinimumValue != null)
                    {
                        rv.MinimumValue = this.MinimumValue;
                        rv.MaximumValue = Int32.MaxValue.ToString();
                        rv.ErrorMessage = "当前输入数据允许最小值为" + this.MinimumValue;
                    }
                }
                rv.Display = ValidatorDisplay.Dynamic;
                // this.Controls.AddAt(0, rv);
            }
            #endregion

            #region  RegularExpressionValidator 验证

            if ((RequiredFieldType != null) && (RequiredFieldType != "") && (RequiredFieldType != "暂无校验"))
            {
                rev.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
                rev.ControlToValidate = this.ID;
                switch ((FieldType)Enum.Parse(typeof(FieldType), RequiredFieldType, true))
                {
                    case FieldType.真实姓名:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入姓名！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : "^[-]?\\d+[.]?\\d*$";
                            rev.ErrorMessage = "数字的格式不正确";
                            break;
                        }
                    case FieldType.固定电话_国家编号:
                        {
                            // rfv.ErrorMessage = "<font color=red>请输入姓名！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : (@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
                            rev.ErrorMessage = "请输入正确国家编号！";
                            break;
                        }
                    case FieldType.固定电话_电话号码:
                        {
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : "\\d{11}";
                            rev.ErrorMessage = "请输入正确电话号码！";
                            break;
                        }
                    case FieldType.固定电话_分机号:
                        {
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : "((\\(\\d{3}\\) ?)|(\\d{3}-))?\\d{3}-\\d{4}|((\\(\\d{3}\\) ?)|(\\d{4}-))?\\d{4}-\\d{4}";
                            rev.ErrorMessage = "请输入正确分机号！";
                            break;
                        }
                    case FieldType.手机:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入手机号码！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : "^\\d{15}$|^\\d{18}$";
                            rev.ErrorMessage = "请输入正确手机号！";
                            break;
                        }
                    case FieldType.电子邮箱:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入E-mail（邮箱）！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$";
                            rev.ErrorMessage = "E-mail（邮箱）格式错误！正确格式如 abc@126.com ";
                            break;
                        }
                    case FieldType.邮编:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入邮编！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
                            rev.ErrorMessage = "邮编只能输入6位数字！";
                            break;
                        }
                    case FieldType.职务:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入职务！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$";
                            rev.ErrorMessage = "职务最大长度为20个字符，10个汉字！";
                            break;
                        }
                    case FieldType.名称:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入名称！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : "^([0-9]|[0-9].[0-9]{0-2}|[1-9][0-9]*.[0-9]{0,2})$";
                            rev.ErrorMessage = "名称最大长度为20个字符，10个汉字！";
                            break;
                        }
                    case FieldType.标题:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入标题！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
                            rev.ErrorMessage = "名称最大长度为100个字符，50个汉字！";
                            break;
                        }
                    case FieldType.编号:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入编号！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "编号格式错误，只能输入英文、数字、部分符号（-/.），不可输入特殊字符和空格，最大长度为30个字符";
                            break;
                        }
                    case FieldType.版本号:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入版本号！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "版本号格式错误，只能输入英文、数字、部分符号（-/.），不可输入特殊字符和空格，最大长度为20个字符";
                            break;
                        }
                    case FieldType.价格:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入价格！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "价格格式错误，价格有数字及”-“号组成，最大长度为15个字符！";
                            break;
                        }
                    case FieldType.身份证:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入身份证号！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "请输入有效身份证号";
                            break;
                        }
                    case FieldType.QQ:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入QQ！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "QQ只能是数字，长度最大15位！";
                            break;
                        }
                    case FieldType.MSN:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入MSN！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "MSN格式错误！正确格式如 abc@hotmail.com ！";
                            break;
                        }
                    case FieldType.网页地址:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入网页地址！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$";
                            rev.ErrorMessage = "网页地址格式错误！";
                            break;
                        }
                    case FieldType.日期:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入日期！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "日期格式错误，正确格式如 2009-5-1 ！";
                            break;
                        }

                    case FieldType.日期时间:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入日期时间！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "日期时间格式错误，正确格式如 2009-5-1 24:00:00 ！";
                            break;
                        }

                    case FieldType.IP地址:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入IP！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "IP格式错误，正确格式如 192.168.0.70 ！";
                            break;
                        }
                    case FieldType.IP地址带端口:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入带端口的IP！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "IP格式错误，正确格式如 192.168.0.70：1234！";
                            break;
                        }
                    case FieldType.地址:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入地址！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "地址最大长度为160个字符或80个汉字！";
                            break;
                        }
                    case FieldType.简介:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入简介！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "简介为4-400个字符，2-200个汉字！";
                            break;
                        }
                    case FieldType.备注:
                        {
                            rfv.ErrorMessage = "<font color=red>请输入备注！</font>";
                            rev.ValidationExpression = (this.ValidationExpression != null) ? this.ValidationExpression : @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{1,5}?$";
                            rev.ErrorMessage = "备注为4-400个字符，2-200个汉字！";
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                isRev = true;
                // this.Controls.AddAt(0, rev);

            }// if  RequiredFieldType 

            #endregion

            if (isRfv)
            { //RequiredFieldValidator
                this.Controls.AddAt(0, rfv);
            }
            if (isCv)
            { //CompareValidator
                this.Controls.AddAt(0, cv);
            }
            if (isRv)
            { //RangeValidator
                this.Controls.AddAt(0, rv);
            }
            if (isRev)
            { //RegularExpressionValidator
                this.Controls.AddAt(0, rev);
            }

        }

        /// <summary>
        /// 输出html,在浏览器中显示控件
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            RenderChildren(writer);
        }

        /// <summary>
        /// 添加属性方法
        /// </summary>
        /// <param name="key">键值,如class等</param>
        /// <param name="valuestr">要绑定的字符串</param>
        public void AddAttributes(string key, string valuestr)
        {
            this.Attributes.Add(key, valuestr);
        }

        /// <summary>
        /// 要进行校验的表达式
        /// </summary>
        [Bindable(false), Category("Behavior"), DefaultValue(""), TypeConverter(typeof(RequiredFieldTypeControlsConverter)), Description("要滚动的对象。")]
        public string RequiredFieldType
        {
            get
            {
                object o = ViewState["RequiredFieldType"];
                return (o == null) ? "" : o.ToString();
            }
            set
            {
                ViewState["RequiredFieldType"] = value;
            }
        }

        private string _maximumValue = null;

        /// <summary>
        /// 最大值属性
        /// </summary>
        [Bindable(true), Category("Appearance"), DefaultValue(null)]
        public string MaximumValue
        {
            get
            {
                return _maximumValue;
            }
            set
            {
                _maximumValue = value;
            }
        }

        private string _minimumValue = null;

        /// <summary>
        /// 最小值属性
        /// </summary>
        [Bindable(true), Category("Appearance"), DefaultValue(null)]
        public string MinimumValue
        {
            get
            {
                return _minimumValue;
            }
            set
            {
                _minimumValue = value;
            }
        }

        /// <summary>
        /// 要表达式是否可以为空
        /// </summary>
        [Bindable(false), Category("Behavior"), DefaultValue("可为空"), TypeConverter(typeof(CanBeNullControlsConverter)), Description("要滚动的对象。")]
        public string CanBeNull
        {
            get
            {
                object o = ViewState["CanBeNull"];
                return (o == null) ? "" : o.ToString();
            }
            set
            {
                ViewState["CanBeNull"] = value;
            }
        }

        /// <summary>
        /// 有效校验表达式
        /// </summary>
        [Bindable(true), Category("Appearance"), DefaultValue("")]
        public string ValidationExpression
        {
            get
            {
                object o = ViewState["ValidationExpression"];
                if (o == null || o.ToString().Trim() == "")
                {
                    return null;
                }
                else
                {
                    return o.ToString().ToLower();
                }
            }
            set
            {
                ViewState["ValidationExpression"] = value;
            }
        }

        private string _controlToCompare;
        /// <summary>
        /// 要比较的控件ID
        /// </summary>
        [Bindable(true), Category("Appearance"), DefaultValue(null)]
        public string ControlToCompare
        {
            get
            {
                return _controlToCompare;
            }
            set
            {
                _controlToCompare = value;
            }
        }
    }

    /// <summary>
    /// 文本框类型
    /// </summary>
    public enum FieldType : int
    {
        暂无校验 = 0,
        真实姓名,
        固定电话_国家编号,
        固定电话_区号,
        固定电话_电话号码,
        固定电话_分机号,
        手机,
        电子邮箱,
        邮编,
        职务,
        名称,
        标题,
        编号,
        版本号,
        价格,
        身份证,
        QQ,
        MSN,
        网页地址,
        日期,
        日期时间,
        IP地址,
        IP地址带端口,
        地址,
        简介,
        备注
    }

    /// <summary>
    /// 下拉列表选项转换器
    /// </summary>
    public class RequiredFieldTypeControlsConverter : StringConverter
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public RequiredFieldTypeControlsConverter() { }

        /// <summary>
        /// 说明要用下拉列表编辑属性 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// 获取标准值列表
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            // ArrayList list = new ArrayList();
            //目前无法按指定顺序显示。
            Type type = typeof(FieldType);
            Array list = Enum.GetNames(type);

            return new StandardValuesCollection(list);
        }

        /// <summary>
        /// return ture的话只能选,return flase可选可填 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }
    }

    /// <summary>
    /// 下拉列表选项转换器
    /// </summary>
    public class CanBeNullControlsConverter : StringConverter
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CanBeNullControlsConverter() { }

        /// <summary>
        /// 下拉列表编辑属性
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// 获取标准值列表
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            ArrayList controlsArray = new ArrayList();
            controlsArray.Add("可为空");
            controlsArray.Add("必填");

            return new StandardValuesCollection(controlsArray);

        }

        /// <summary>
        /// return ture的话只能选,return flase可选可填 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }
    }
}
